import { _decorator, Component, Node, Prefab, CCInteger, UIComponent, UITransform, instantiate, Vec3, find, Label } from 'cc';
import KlotskiSolve from './KlotskiSolve';
const { ccclass, property, type } = _decorator;
/**
 * zongyuan.yang
 * 游戏控制类
 */

@ccclass('GameUI')
export class GameUI extends Component {
    @type(Prefab)
    item: Prefab = null;
    @type(CCInteger)
    line: number = 0;
    @type(CCInteger)
    row: number = 0;
    @type(Node)
    panel: Node = null;
    @type(Node)
    solvingUI: Node = null;

    private mKlotskiSolve: KlotskiSolve = null;
    private mItemS: Array<Node> = null;
    private mResult:Array<any>=null;

    start() {
        this.mKlotskiSolve = new KlotskiSolve(this.klotskiSolveCall.bind(this));
        this.solvingUI.active=false;
    }

    update(deltaTime: number) {

    }
    /**
     * 初始化map
     */
    public initMapEvent(): void {
        this.panel.destroyAllChildren();
        this.mItemS = [];
        this.mResult=null;
        //初始化数据
        let data: Array<number> = [];
        for (let i = 0; i < this.line * this.row - 1; i++) {
            data.push(i + 1);
        }
        data.push(-1);
        //打乱数据
        for (let i = 0; i < 100; i++) {
            let index0: number = Math.floor(Math.random() * this.line * this.row);
            let index1: number = Math.floor(Math.random() * this.line * this.row);
            if (index0 != index1) {
                let temp = data[index0];
                data[index0] = data[index1];
                data[index1] = temp;
            }
        }
        this.mKlotskiSolve.init(this.line, this.row, data);
        console.log(data);
        let panelWidth: number = this.panel.getComponent(UITransform).width;
        let itemWidth: number = this.panel.getComponent(UITransform).width / this.line;
        let itemScale: number = itemWidth / 150;
        for (let i = 0; i < data.length; i++) {

            let line: number = i % this.line;
            let row: number = Math.floor(i / this.line);
            let x: number = -panelWidth / 2 + (line) * itemWidth + itemWidth / 2;
            let y: number = panelWidth / 2 - (row) * itemWidth - itemWidth / 2;
            let item: Node = instantiate(this.item);
            item.setPosition(new Vec3(x, y, 0));
            this.panel.addChild(item);
            this.mItemS.push(item);
            item.setScale(new Vec3(itemScale, itemScale, itemScale));
            if (data[i] != -1) {
                find("label", item).getComponent(Label).string = data[i] + "";
            } else {
                find("label", item).getComponent(Label).string = "";
                find("panel", item).active = false;
            }
        }
    }

    public solveEvent():void{
        this.solve();
    }
    private async solve():Promise<void>{
        let result: Array<any> = await this.mKlotskiSolve.solve();
        if (result && result.length > 0) {
            this.mResult=result;
        }
    }

    public selfArrangeEvent():void{
        if(!this.mResult) return;
        let index: number = 0;
        this.move(index, this.mResult);
    }
    public move(index: number, data: Array<any>): void {
        if (index >= data.length) {
            return;
        }
        console.log(index, data.length);
        let itemIndex: number = data[index]["index"];
        let itemDic: number = data[index]["dic"];
        let item: Node = this.mItemS[itemIndex];

        let line: number = itemIndex % this.line;
        let row: number = Math.floor(itemIndex / this.line);
        let targetIndex: number = 0;
        if (itemDic == 0) {
            targetIndex = (row - 1) * this.line + line;
        } else if (itemDic == 1) {
            targetIndex = (row + 1) * this.line + line;
        } else if (itemDic == 2) {
            targetIndex = row * this.line + (line - 1);
        } else if (itemDic == 3) {
            targetIndex = row * this.line + (line + 1);
        }
        let blockItem: Node = this.mItemS[targetIndex];
        let blockPos = new Vec3(0, 0, 0);
        Vec3.copy(blockPos, blockItem.getPosition());

        blockItem.position = item.position;
        item.position = blockPos;

        let tempNode: Node = item;

        this.mItemS[itemIndex] = blockItem;
        this.mItemS[targetIndex] = item;
        this.scheduleOnce(() => {
            index++;
            this.move(index, data);
        }, 0.25);

    }

    private klotskiSolveCall(): void {

    }

}
